package com.banggood.upms.web;

import com.banggood.silk.web.api.common.PagedResult;
import com.banggood.upms.bean.po.Permission;
import com.banggood.upms.bean.po.SystemPo;
import com.banggood.upms.bean.vo.PermissionVo;
import com.banggood.upms.bean.vo.SystemModuleVo;
import com.banggood.upms.bean.vo.SystemVo;
import com.banggood.upms.bean.vo.search.SysPermSearchCriteria;
import com.banggood.upms.bean.vo.validation.AddGroup;
import com.banggood.upms.service.PermissionService;
import com.banggood.upms.service.SystemService;
import com.banggood.upms.converter.PermissionConverter;
import com.banggood.upms.converter.SystemConverter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author XuChuangFeng
 */
@RequestMapping("/perm")
@RestController
@Api(value = "权限管理", description = "权限数据是对应系统的开发者维护的，upms只提供检索功能，不提供新增，修改，删除功能。")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;
    @Autowired
    private SystemService systemService;
    @Autowired
    private PermissionConverter permissionConverter;
    @Autowired
    private SystemConverter systemConverter;

    /**
     * 根据系统ID查询该系统下面的所有权限，不传入系统ID则返回所有系统及其权限
     * @param criteria
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/search", method = RequestMethod.POST)
    @ApiOperation(value = "权限检索", notes = "不传入systemId，则返回所有系统及其所有权限。")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "systemId", value = "系统主键，不传入则检索全部系统", required = false, paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isActive", value = "是否激活", required = false, paramType = "query", dataType = "String", allowableValues = "ACTIVE,INACTIVE"),
    })
    public PagedResult<SystemVo> search(@Validated({AddGroup.class}) SysPermSearchCriteria criteria) throws Exception {
        List<Permission> permissions = permissionService.selectByCriteria(criteria);
        List<SystemPo> systems = systemService.selectByCriteria(criteria);
        List<SystemVo> systemVos = systemConverter.outputConverter(systems);
        //取记录总条数
        Map<String, List<SystemModuleVo>> systemModuleMap = permissionConverter.outputConverter(permissions);
        for(SystemVo systemVo : systemVos) {
            systemVo.setModules(systemModuleMap.get(systemVo.getSystemId()));
        }
        PagedResult<SystemVo> result = new PagedResult<>(systemVos, (long)systemVos.size());
        return result;
    }

    @RequestMapping(value = "/view", method = RequestMethod.GET)
    @ApiOperation(value = "根据权限ID获取权限详情", notes = "GET请求")
    public PermissionVo selectByPrimaryKey(@RequestParam String id) throws Exception {
        Permission permission = permissionService.selectByPrimaryKey(id);
        return permission != null ? permissionConverter.singleOutputConverter(permission) : null;
    }
}